package com.zhangshujie.ShoppingSystem;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileManager {
    private static final String ADMIN_FILE = "admins.txt";
    private static final String CUSTOMER_FILE = "customers.txt";
    private static final String PRODUCT_FILE_NAME = "product.txt";

    public void addAdmin(Admin admin) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(ADMIN_FILE, true))) {
            writer.write(admin.toString());
            writer.newLine();
        } catch (IOException e) {
            System.out.println("Error writing to admin file: " + e.getMessage());
        }
    }
    public List<Admin> readAdmins() {
        List<Admin> admins = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(ADMIN_FILE))) {
            String line;
            while ((line = reader.readLine()) != null) {
                admins.add(parseAdmin(line));
            }
        } catch (IOException e) {
            System.out.println("Error reading admin file: " + e.getMessage());
        }
        return admins;
    }
    private Admin parseAdmin(String line) {
        String[] parts = line.split(",");
        return new Admin(parts[0], parts[1], parts[2], parts[3], parts[4]);
    }
    public void addCustomer(Customer customer) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(CUSTOMER_FILE, true))) {
            writer.write(customer.toString());
            writer.newLine();
        } catch (IOException e) {
            System.out.println("Error writing to customer file: " + e.getMessage());
        }
    }
    public List<Customer> readCustomers() {
        List<Customer> customers = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(CUSTOMER_FILE))) {
            String line;
            while ((line = reader.readLine()) != null) {
                customers.add(parseCustomer(line));
            }
        } catch (IOException e) {
            System.out.println("Error reading customer file: " + e.getMessage());
        }
        return customers;
    }
    private Customer parseCustomer(String line) {
        String[] parts = line.split(",");
        return new Customer(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]);
    }
    public void saveProducts(List<Product> products) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(PRODUCT_FILE_NAME))) {
            oos.writeObject(products);
        } catch (IOException e) {
            System.out.println("保存商品信息时出错：" + e.getMessage());
        }
    }

    public List<Product> loadProducts() {
        List<Product> products = new ArrayList<>();
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRODUCT_FILE_NAME))) {
            products = (List<Product>) ois.readObject();
        } catch (FileNotFoundException e) {
            System.out.println("商品信息文件未找到，创建新的文件。");
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载商品信息时出错：" + e.getMessage());
        }
        return products;
    }
}
